package com.bhs.sansonglogistics.ui.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.Initialize;
import com.bhs.sansonglogistics.bean.GlobalData;
import com.bhs.sansonglogistics.dialog.MyClickListener;
import com.bhs.sansonglogistics.dialog.PrivacyAgreementDialog;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.home.MainActivity;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.OneClickLoginUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.bhs.sansonglogistics.utils.StatusBarUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 启动页
 */
@SuppressLint("CustomSplashScreen")
public class SplashActivity extends BaseActivity implements NetCallBack {

    private ImageView mIvAdvertising;
    private TextView mTvSkip;

    @Override
    public int initLayout() {
        return R.layout.activity_splash;
    }

    public void initView() {
        mIvAdvertising = findViewById(R.id.iv_advertising);
        StatusBarUtil.setTranslucentStatus(this, true);
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            StatusBarUtil.setStatusBarColor(this, 0x55000000);
        }
        boolean privacyPolicy = SharedPreferencesUtils.getBoolean(this, "privacy", false);
        if (!privacyPolicy) {
            PrivacyAgreementDialog();
        } else {
            advertising();
        }
        mTvSkip = findViewById(R.id.tv_skip);
        mTvSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String token = SharedPreferencesUtils.getString(SplashActivity.this, "token");
                if (TextUtils.isEmpty(token)) {
                    startActivity(new Intent(SplashActivity.this, PhoneNumberLoginActivity.class));
                    finish();
                } else {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }
            }
        });
    }


    private void PrivacyAgreementDialog() {
        PrivacyAgreementDialog privacyAgreementDialog = new PrivacyAgreementDialog(this);
        BasePopupView baseDialog = new XPopup.Builder(this).asCustom(privacyAgreementDialog).show();
        baseDialog.show();

        privacyAgreementDialog.setMyClickListener(new MyClickListener() {
            @Override
            public void onClick(View v) {
                Initialize.init(SplashActivity.this);
                advertising();
            }
        });
    }


    private void advertising() {
        oneClickLoginUtils = new OneClickLoginUtils(SplashActivity.this);
        String url = "https://baohusan-uisource.oss-cn-shanghai.aliyuncs.com/mp-transport/index/advertising.png";
        Glide.with(this)
                .load(url)
                .skipMemoryCache(true) // 不使用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                .into(new SimpleTarget<Drawable>(MyUtils.getScreenWidth(this), MyUtils.getScreenHeight(this)) {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mIvAdvertising.setImageDrawable(resource);
                                gotoPage();
                            }
                        }, 500); // 延时1秒

                    }
                });
    }

    OneClickLoginUtils oneClickLoginUtils;

    @Override
    public void initData() {

    }

    private void gotoPage() {
        //图片加载完成
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String token = SharedPreferencesUtils.getString(SplashActivity.this, "token");
                if (TextUtils.isEmpty(token)) {
                    oneClickLoginUtils.pullPage();
                } else {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }
                mTvSkip.setVisibility(View.VISIBLE);
            }
        }, 1000); // 延时1秒
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {

    }

    @Override
    public void onFailure(int requestCode) {

    }
}